var uart = require('uart');
var gpio = require('gpio');

/* string convert to decimal array*/
function StrToDecArray(str) {
    var obj = {
        '0':48,
        '1':49,
        '2':50,
        '3':51,
        '4':52,
        '5':53,
        '6':54,
        '7':55,
        '8':56,
        '9':57,
    }
    return str.split('').map(function(i) {
        if(obj[i]) {
            return obj[i]
        }
        return i.charCodeAt()
    })
}

function ArrayToString(fileData){
  var dataString = "";
  for (var i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData[i]);
  }
  return dataString;
}

function stringToBytesArray(str) {
    var ch, st, re = [];
    for (var i = 0; i < str.length; i++ ) { 
        ch = str.charCodeAt(i);
        st = [];
       do {
            st.push(ch & 0xFF);
            ch = ch >> 8;
        }
        while ( ch );
        re = re.concat(st.reverse());
    }
    return re;
}

/*------------------------------------------------------------- */

/* open RS232 */
var serial1 = uart.open({
    id: 'serial1',
    success: function() {
        console.log('open RS232 success')
    },
    fail: function() {
        console.log('open RS232 fail')
    }
 });

/* open GPS */
var serial2 = uart.open({
    id: 'serial2',
    success: function() {
        console.log('open GPS uart success')
    },
    fail: function() {
        console.log('open GPS uart fail')
    }
});

/* open RS485 */
var serial3 = uart.open({
    id: 'serial3',
    success: function() {
        console.log('open RS485 success')
    },
    fail: function() {
        console.log('open RS485 fail')
    }
});

var msgbuf = [0x41, 0x42, 0x43, 0x44, 0x61, 0x62, 0x63, 0x64, 0x2B, 0X2B];
var exit = "exit"
var cnt = 3;

/* uart send only supports array data, not supoort string */
var intervalHandled = setInterval(function(){
    cnt--;
	if(cnt <= 0){
        clearInterval(intervalHandled);
    }
    serial1.write(msgbuf);
    // serial2.write(msgbuf);
    serial3.write(msgbuf);
},2000);

/* uart receive test */
serial1.on('data', function(data) {
    console.log(ArrayToString(data));
    serial1.write(data);
    if (ArrayToString(data) === exit) {
      console.log('rs232: test finish')
      serial1.close();
    }
  });

/* gps receive test */
serial2.on('data', function(data) {
    console.log(ArrayToString(data));
    serial1.write(data);
});

/* rs485 receive test */
serial3.on('data', function(data) {
    console.log(ArrayToString(data));
    serial3.write(data);
    if (ArrayToString(data) === exit) {
      console.log('rs485: test finish')
      serial3.close();
    }
  });

/*------------------------------------------------------------- */

  var gps_pwr = gpio.open({
    id: 'gps_pwr',
    success: function() {
        console.log('gps_pwr open success')
    },
    fail: function() {
        console.log('gps_pwr open failed')
    }
});

var gps_rst = gpio.open({
    id: 'gps_rst',
    success: function() {
        console.log('gps_rst open success')
    },
    fail: function() {
        console.log('gps_rst open failed')
    }
});

var gps_standby = gpio.open({
    id: 'gps_standby',
    success: function() {
        console.log('gps_standby open success')
    },
    fail: function() {
        console.log('gps_standby open failed')
    }
});


/* gps module l76-lb power on sequence */
function gps_l76_init() {
    gps_pwr.writeValue(1);
    gps_standby.writeValue(0);
    gps_rst.writeValue(1);
    setTimeout(function(){
        gps_rst.writeValue(0);
        console.log('gps module l76-lb init finish')
    },100);
}

var cnt2 = 5;
var gps_state_require = "$PMTK255,1*2D\r\n";

gps_l76_init();

/* uart send only supports array data, not supoort string */
var intervalHandled2 = setInterval(function(){
    cnt2--;
	if(cnt2 <= 0){
        clearInterval(intervalHandled2);
    }
    serial2.write(StrToDecArray(gps_state_require));
    console.log(gps_state_require);
},3000);

console.log('uart: test start------------------')